package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 获取设备数据分页结果
     * @param pageQueryDto
     * @return
     */
    @Override
    public PageResponse<DeviceDataVo> getDeviceDataList(DeviceDataPageReqDto pageQueryDto) {
        if (pageQueryDto == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }

        PageHelper.startPage(pageQueryDto.getPageNum(),pageQueryDto.getPageSize());

        //分页查询设备上报数据
        pageQueryDto.setStartDate(LocalDateTimeUtil.of(pageQueryDto.getStartTime()));
        pageQueryDto.setEndDate(LocalDateTimeUtil.of(pageQueryDto.getEndTime()));
        List<DeviceDataVo> deviceDataVos = deviceDataMapper.selectDeviceDataPage(pageQueryDto);

        Page<DeviceDataVo> page = (Page<DeviceDataVo>) deviceDataVos;

        return PageResponse.of(page,DeviceDataVo.class);
    }
}
